package renpyas3.model.structs
{
	import br.com.stimuli.loading.BulkLoader;
	import org.osflash.thunderbolt.Logger;
	
	/**
	 * parses the options
	 */
	public class RenpyOptions
	{
		private var _screenWidth:Number = 800;
		private var _screenHeight:Number = 600;
		private var _bgImageFilename:String;
		private var _vectorOfLines:Vector.<String>;
		
		
		public function RenpyOptions(vectorOfLines:Vector.<String>) 
		{
			_vectorOfLines = vectorOfLines;

			//trimAllLines();
			parseOptions();
		}
		
		
		/**
		 * remove whitespace at the begginning and end
		 * in all lines
		 */
		private function trimAllLines():void
		{
			for (var i:uint = 0; i < _vectorOfLines.length ; i++ )
			{
				_vectorOfLines[i] = f_trim(_vectorOfLines[i]);
			}			
		}
		
		/**
		 * remove whitespace at the begginning and end
		 */
		public static function f_trim(str:String):String
		{
			//remove whitespace
			return(str.replace(/^\s+|\s+$/g, ""));
		}
		
		
		/**
		 * go through all the lines finding some data
		 */
		private function parseOptions():void
		{
			var firstWord:String;
			var firstSpace:int;
			var currLine:String
			
			//remove whitespace at the begginning and end
			for (var i:uint = 0; i < _vectorOfLines.length ; i++ )
			{
				//Logger.info(s);
				if (_vectorOfLines[i].indexOf("mm_root") != -1)
				{
					parseOptionsBg(_vectorOfLines[i]);
				}
				else if (_vectorOfLines[i].indexOf("config.screen_width") != -1)
				{
					parseOptionsScreenWidth(_vectorOfLines[i]);
				}
				else if (_vectorOfLines[i].indexOf("config.screen_height") != -1)
				{
					parseOptionsScreenHeight(_vectorOfLines[i]);
				}
			}
		}
		
		private function parseOptionsBg(line:String):void
		{
			var equalsSignIndex:int = line.indexOf("=");
			var dotIndex:int = line.indexOf(".");
			if (equalsSignIndex == -1)
			{
				Logger.error("RenpyOptions::parseOptionsBg ERROR trying to find = sign in:"+line);
			}
			else if (dotIndex == -1)
			{
				Logger.error("RenpyOptions::parseOptionsBg ERROR trying to find filename/extension in:"+line);
			}
			else
			{			
				_bgImageFilename = line.substr(equalsSignIndex+1);
				_bgImageFilename = _bgImageFilename.replace(",", "");
				_bgImageFilename = _bgImageFilename.replace("\'", "");
				_bgImageFilename = _bgImageFilename.replace("\'", "");
				_bgImageFilename = _bgImageFilename.replace("\"", "");
				_bgImageFilename = _bgImageFilename.replace("\"", "");
				_bgImageFilename = f_trim(_bgImageFilename);
			}
			trace("_bgImageFilename:",_bgImageFilename);
		}
		

		private function parseOptionsScreenWidth(line:String):void
		{
			var equalsSignIndex:int = line.indexOf("=");
			if (equalsSignIndex == -1)
			{
				Logger.error("RenpyOptions::parseOptionsScreenWidth ERROR trying to find = sign in:"+line);
			}
			else
			{			
				var screenWidthStr:String = line.substr(equalsSignIndex+1);
				screenWidthStr = screenWidthStr.replace(",", "");
				screenWidthStr = f_trim(screenWidthStr);
				_screenWidth = Number(screenWidthStr);
			}
			trace("_screenWidth:", _screenWidth);
		}

		private function parseOptionsScreenHeight(line:String):void
		{
			var equalsSignIndex:int = line.indexOf("=");
			if (equalsSignIndex == -1)
			{
				Logger.error("RenpyOptions::parseOptionsScreenHeight ERROR trying to find = sign in:"+line);
			}
			else
			{			
				var screenHeightStr:String = line.substr(equalsSignIndex+1);
				screenHeightStr = screenHeightStr.replace(",", "");
				screenHeightStr = f_trim(screenHeightStr);
				_screenHeight = Number(screenHeightStr);
			}
			trace("_screenHeight:", _screenHeight);
		}
		
		public function get screenWidth():Number { return _screenWidth; }
		
		public function get screenHeight():Number { return _screenHeight; }
		
		public function get bgImageFilename():String { return _bgImageFilename; }

	}
}